package com.sgb.jicai.view.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.tabs.TabLayout;
import com.sgb.jicai.R;
import com.sgb.jicai.databinding.ActivityXufangFaqiBinding;
import com.sgb.jicai.viewmodel.XuFangFaQiModel;
import com.sgb.link.entity.EventConstant;
import com.swgk.core.base.AppActivity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.jicai.event.JiCaiEventEntity;
import com.sgb.jicai.view.fragment.JiCaiXuFangFaQiFragment;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.List;

/**
 * 前台需方发起
 * 12-16 改版使用新的页面
 */
public class XuFangFaQiActivity extends AppActivity implements View.OnClickListener {

    XuFangFaQiModel model;
    ActivityXufangFaqiBinding binding;
    private ArrayList<Fragment> mFragments;
    private String typeNo ="";


    public static void start(Context context,String type){
        Intent intent = new Intent();
        intent.putExtra("type",type);
        intent.setClass(context, XuFangFaQiActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        super.initView();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        binding = DataBindingUtil.setContentView(XuFangFaQiActivity.this, R.layout.activity_xufang_faqi);
        model = new ViewModelProvider(this).get(XuFangFaQiModel.class);
        model.setActivity(XuFangFaQiActivity.this);
        binding.ivBack.setOnClickListener(this::onMultiClick);
        binding.tvSearch.setOnClickListener(this::onMultiClick);
    }

    @Override
    protected void init() {
        super.init();
        model.groupBuyClassList();

    }

    @Override
    public void onMultiClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.tv_search) {//把搜索词发送到子fragment中刷新列表
            EventBus.getDefault().post(new JiCaiEventEntity(EventConstant.EVENT_JICAI_HOME_FOUR_TYPE_SEARCH, binding.editSearch.getText().toString().trim()));
        }
    }

    public void showEmptyView() {
        binding.emptyView.setVisibility(View.VISIBLE);
    }
    //二级tab数据
    public void updateSecondTabData(List<PickSelectEntity> data) {
        if (data == null || data.size() == 0) {
            showEmptyView();
            return;
        }
        //添加  全部
        PickSelectEntity entity = new PickSelectEntity();
        entity.setProjectscale("全部");
        entity.setNo("");
        data.add(0, entity);
        updataTab(data);
    }

    private void updataTab(List<PickSelectEntity> dataTabSecond) {
        if (null == dataTabSecond||dataTabSecond.size()==0) {
            return;
        }
        String[] mTitles = new String[dataTabSecond.size()];
        mFragments = new ArrayList<>();
        for (int i = 0; i < dataTabSecond.size(); i++) {//类型：商品集采、机械集采、设备集采…………
            mTitles[i]=(dataTabSecond.get(i).getProjectscale());
            mFragments.add(JiCaiXuFangFaQiFragment.newInstance(dataTabSecond.get(i).getNo().isEmpty()?0:Integer.valueOf(dataTabSecond.get(i).getNo()),getIntent().getStringExtra("type"),"1","2",""));
        }
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        binding.viewPager.setOffscreenPageLimit(mTitles.length);
        binding.viewPager.setAdapter(mPagerAdapter);
        binding.tabLayout.setupWithViewPager(binding.viewPager);
       // binding.tabLayout.setTabMode(TabLayout.MODE_FIXED);
        binding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tabDataList();
                initializePop();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void initializePop() {
        Fragment fragment = mFragments.get(binding.viewPager.getCurrentItem());
        if (fragment instanceof JiCaiXuFangFaQiFragment) {
            ((JiCaiXuFangFaQiFragment) fragment).initializePop();
        }
    }

    private void tabDataList() {
        Fragment fragment = mFragments.get(binding.viewPager.getCurrentItem());
        if (fragment instanceof JiCaiXuFangFaQiFragment) {
            ((JiCaiXuFangFaQiFragment) fragment).getData();
        }
    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(JiCaiEventEntity event) {
        if (event.getCode() == EventConstant.EVENT_JICAI_TOP_REFRESH_VISIBLE) {
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}
